package com.swas.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swas.entity.Base;
import com.swas.entity.BaseDevice;
import com.swas.entity.BaseStock;
import com.swas.entity.BaseType;
import com.swas.service.BaseService;
import com.swas.util.ResultVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lxysky
 */
@RestController
@RequestMapping("/base")
public class BaseController {

    @Autowired
    private BaseService baseService;

    @GetMapping("/selectBaseStockById/{id}")
    public ResultVO selectBaseById(@PathVariable("id") Integer id) {
        BaseStock baseStock = baseService.selectBaseStockById(id);
        return ResultVO.success("查询成功", baseStock);
    }

    @GetMapping("/selectBaseTypeByName")
    public ResultVO selectBaseTypeByName(@Param("name") String name) {
        BaseType baseType = baseService.selectBaseTypeByName(name);
        return ResultVO.success("查询成功", baseType);
    }

    @PutMapping("/updateBaseTypeById")
    public ResultVO updateBaseTypeById(@RequestBody BaseType baseType) {
        boolean flag = baseService.updateBaseTypeById(baseType);
        if (flag) {
            return ResultVO.success("修改成功");
        } else {
            return ResultVO.fail("修改失败");
        }
    }

    @GetMapping("/insertBase")
    public ResultVO insertBase(@Param("baseid") String baseid, @Param("name") String name, @Param("address") String address, @Param("longitude") Double longitude, @Param("latitude") Double latitude, @Param("usetime") Date usetime, @Param("describe") String describe) {
        BaseType baseType = new BaseType();
        baseType.setBaseid(Integer.parseInt(baseid));
        baseType.setName(name);
        baseType.setAddress(address);
        baseType.setLongitude(longitude);
        baseType.setLatitude(latitude);
        baseType.setUsetime(usetime);
        baseType.setDescribe(describe);
        boolean flag = baseService.insertBase(baseType);
        if (flag) {
            return ResultVO.success("新增成功");
        } else {
            return ResultVO.fail("新增失败");
        }
    }

    @PostMapping("/insertBaseStock")
    public ResultVO insertBaseStock(@RequestBody BaseStock baseStock) {
        boolean flag = baseService.insertBaseStock(baseStock);
        if (flag) {
            return ResultVO.success("新增成功");
        } else {
            return ResultVO.fail("新增失败");
        }
    }

    @PutMapping("/updateDeviceStatus")
    public ResultVO updateDeviceStatus(@RequestBody BaseDevice baseDevice) {
        boolean flag = baseService.updateDeviceStatus(baseDevice);
        if (flag) {
            return ResultVO.success("修改成功");
        } else {
            return ResultVO.fail("修改失败");
        }
    }

    @PostMapping("/addOneDevice")
    public ResultVO addOneDevice(@RequestBody BaseDevice baseDevice) {
        boolean flag = baseService.addOneDevice(baseDevice);
        if (flag) {
            return ResultVO.success("新增成功");
        } else {
            return ResultVO.fail("新增失败");
        }
    }

    @GetMapping("/selectBaseDevice/{id}")
    public ResultVO selectBaseDevice(@PathVariable("id") Integer id) {
        List<BaseDevice> list = baseService.selectBaseDevice(id);
        return ResultVO.success("查询成功", list);
    }

    @GetMapping("/selectBaseAll")
    public ResultVO selectBaseAll() {
        List<Base> list = baseService.selectBaseAll();
        return ResultVO.success("查询成功", list);
    }

    @DeleteMapping("/deleteBaseStockById/{id}")
    public ResultVO deleteBaseStockById(@PathVariable("id") Integer id) {
        boolean flag = baseService.deleteBaseStockById(id);
        return ResultVO.success("删除成功");
    }

    @RequestMapping("/selectBaseStockAll")
    public ResultVO selectBaseStockAll(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BaseStock> list = baseService.selectBaseStockAll();
        PageInfo<BaseStock> pageInfo = new PageInfo<>(list);
        return ResultVO.success("查询成功", pageInfo);
    }

}
